package com.cnpc.fyviewlibrary.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.cnpc.fyviewlibrary.R;


/**
 * @author bingju
 *
 */
public class CommonDialog extends Dialog {

	private TextView tv_title,tv_content,tv_confirm,tv_cancel;
	private View selfview;
	private int layout_id = 0;

//	private TextView tv_hinttitle, tv_content, tv_cancel, tv_confirm;
	private String title;
	private String content;
	private String positive;
	private String negative;
	private OnClickListener positiveClickListener;
	private OnClickListener negativeClickListener;
	private OnClickListener selfClickListener;

	public interface OnClickListener {
		void onClick();
	}

	public CommonDialog(Context context, int layout_id) {
		super(context);
		this.layout_id = layout_id;
	}

	public CommonDialog(Context context, int layout_id, boolean cancelable, OnCancelListener cancelListener) {
		super(context, cancelable, cancelListener);
		this.layout_id = layout_id;
	}

	public CommonDialog(Context context, int layout_id, int theme) {
		super(context, theme);
		this.layout_id = layout_id;
	}

//	public void setDialogTitle(String title) {
//		this.title = title;
//	}
//
	public CommonDialog setDialogTitle(String title) {
		this.title = title;
		return this;
	}
	public CommonDialog setDialogContent(String content) {
		this.content = content;
		return this;
	}

	public CommonDialog setDialogPositiveButton(String positive) {
		this.positive = positive;
		return this;
	}

	public CommonDialog setDialogNegativeButton(String negative) {
		this.negative = negative;
		return this;
	}

	public CommonDialog setPostitveButton(String positive, OnClickListener onclickListener) {
		this.positive = positive;
		this.positiveClickListener = onclickListener;
		return this;
	}

	public CommonDialog setNegativeButton(String negative, OnClickListener onclickListener) {
		this.negative = negative;
		this.negativeClickListener = onclickListener;
		return this;
	}
	public CommonDialog setSelfClickListener(OnClickListener onclickListener) {
		this.selfClickListener = onclickListener;
		return this;
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(layout_id);
//		this.setCancelable(true);
		tv_title = (TextView) findViewById(R.id.tv_title);
		tv_content = (TextView) findViewById(R.id.tv_content);
		tv_cancel = (TextView) findViewById(R.id.tv_cancel);
		tv_confirm = (TextView) findViewById(R.id.tv_confirm);
		selfview = findViewById(R.id.seftview);//// TODO: 2016/7/23
		if (!TextUtils.isEmpty(title) && tv_title != null)
			tv_title.setText(title);
		if (!TextUtils.isEmpty(content) && tv_content != null)
			tv_content.setText(content);
		if (!TextUtils.isEmpty(positive) && tv_confirm != null)
			tv_confirm.setText(positive);
		if (!TextUtils.isEmpty(negative) && tv_cancel != null)
			tv_cancel.setText(negative);
		if (tv_cancel != null) {
			tv_cancel.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					if (negativeClickListener != null)
						negativeClickListener.onClick();
					dismiss();
				}
			});
		} else {
			throw new IllegalArgumentException("*在自定义dialog布局中的 negative 事件id为 R.id.tv_cancel*");
		}
		if (tv_confirm != null) {
			tv_confirm.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					if (positiveClickListener != null)
						positiveClickListener.onClick();
					dismiss();
				}
			});
		} else {
			throw new IllegalArgumentException("*在自定义dialog布局中的 positive 事件id为 R.id.tv_confirm*");
		}

		if (selfview != null) {
			selfview.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					if (selfClickListener != null) {
						selfClickListener.onClick();
					}
					dismiss();
				}
			});
		}

	}
}
